package com.shg.web.action;

import java.io.IOException;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Controller;

import com.shg.bean.Users;
import com.shg.service.UsersService;
import com.shg.web.mail.action.MailAction;
import com.shg.web.mail.action.MailInterface;
import com.shg.web.tools.MD5;
import com.shg.web.tools.TimerTool;
import com.shg.web.tools.WebTool;

@Controller
public class UsersAction extends MailAction  implements MailInterface{

	private String message="书皇馆";

	private Users user;
	//
	private String email;
	//
	private String passWord;
	
	//
	private String path;
	//随机数存储
	private int validate=0;
	//
	//页面传过来的验证吗
	private int check;
	@Resource
	private UsersService usersService;
	
	public String checkUser(){
		if(usersService.getCheck(user.getUserEmail())){
			this.message="你的Email有效";
		}else{
			this.message="该Email已经被注册";
		}
		return "checkUser";
	}
	/**
	 * 注册
	 */
	public String register(){
		if(validate==check && user!=null){
			user.setPassWord(MD5.MD5Encode(user.getPassWord()));
			usersService.insert(user);
			this.message="恭喜你注册成功，开始我的学习生涯！";
			WebTool.saveUser(user);
		}else{
			this.message="你输入的确认不正确，请重新发送邮件回去验证码";
		}
		return "register";
	}
	/**
	 * 电子邮件发送
	 * @return
	 */
	@Override
	public String validate() {
		validate = (int) (Math.random()*99999);
		//定时删除
		new TimerTool(this);
		sendMail(user.getUserEmail(), "欢迎加入书皇", "尊敬的：\n"+
													"您好！\n"+
													"书皇网欢迎您\n"+
													"您的注册验证码是："+validate+"\n"+
													"本邮件由系统自动发送，请勿回复!");
		this.message="注册验证码已经发到你的邮箱，请登录你的邮箱获取验证码";
		return "validate";
	}
	/**
	 * 用户注册界面
	 * @return
	 */
	public String registerUI(){
		return "registerUI";
	}
	/**
	 * 用户登录界面
	 * @return
	 */
	public String loginUI(){
		return "loginUI";
	}
	/**
	 * 用户登录
	 * @return
	 */
	public String login(){
		Users user;
		try {
			user = usersService.getLogin(email, passWord);
			if(user==null){
				this.message="用户名或者密码无效,请检查后再登录!";
				return "loginUI";
			}else{
				try {
					WebTool.saveUser(user);
					ServletActionContext.getResponse().sendRedirect(path);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		} catch (Exception e1) {
			e1.printStackTrace();
			this.message="操作发生异常";
			return "error";
		}
		return null;
	}
	/**
	 * 用户登录
	 * @return
	 */
	public String userLogin(){
		 try {
			user = usersService.getLogin(email, passWord);
		} catch (Exception e) {
			e.printStackTrace();
			this.message="操作发生异常";
			return "error";
		}
		if(user==null){
			this.message="用户名或者密码无效,请检查后再登录!";
		}else{
			WebTool.saveUser(user);
			this.message="登录成功";
			return "success";
		}
		return "userLogin";
	}
	/**
	 * 用户登录
	 * @return
	 */
	public String stowLogin(){
		 try {
			user = usersService.getLogin(email, passWord);
		} catch (Exception e) {
			e.printStackTrace();
			this.message="操作发生异常";
			return "error";
		}
		if(user==null){
			this.message="用户名或者密码无效,请检查后再登录!";
		}else{
			WebTool.saveUser(user);
			this.message="登录成功";
			return "success";
		}
		return "stowLogin";
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
	public Users getUser() {
		return user;
	}
	public void setUser(Users users) {
		this.user = users;
	}
	public UsersService getUsersService() {
		return usersService;
	}
	public void setUsersService(UsersService usersService) {
		this.usersService = usersService;
	}
	public String getPath() {
		return path;
	}
	public void setPath(String path) {
		this.path = path;
	}
	public int getValidate() {
		return validate;
	}
	public void updateValidate(int validate) {
		this.validate=validate;
	}
	public int getCheck() {
		return check;
	}
	public void setCheck(int check) {
		this.check = check;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getPassWord() {
		return passWord;
	}
	public void setPassWord(String passWord) {
		this.passWord = passWord;
	}
}
